function grayPixle(pix) {
  return pix[0] * 0.299 + pix[1] * 0.587 + pix[2] * 0.114;
};

export function overwriteImageData(data) {
  let sendWidth = data.width,
    sendHeight = data.height;
  let width1;
  if (sendWidth%8==0){
    width1 = sendWidth/8;
  }else{
    width1 = sendWidth / 8+1;
  }
  const threshold = data.threshold || 180;
  let sendImageData = new ArrayBuffer((sendWidth * sendHeight) / 8);
  sendImageData = new Uint8Array(sendImageData);
  let pix = data.imageData;
  const part = [];
  let index = 0;
  
  for (let i = 0; i < pix.length; i += 32) {
    //横向每8个像素点组成一个字节（8位二进制数）。
    for (let k = 0; k < 8; k++) {
      const grayPixle1 = grayPixle(pix.slice(i + k * 4, i + k * 4 + (4 - 1)));
      //阈值调整
      if (grayPixle1 > threshold) {
        //灰度值大于threshold 为白色 为第k位0不打印
        part[k] = 0;
      } else {
        part[k] = 1;
      }
    }
    let temp = 0;
    for (let a = 0; a < part.length; a++) {
      temp += part[a] * Math.pow(2, part.length - 1 - a);
    }
    //一个字节是8位的二进制数，part这个数组存的0和1就是二进制的0和1，传输到打印的位图数据的一个字节是0-255之间的十进制数，以下是用权相加法转十进制数，理解了这个就用上面的for循环替代了
    // const temp =
    //     part[0] * 128 +
    //     part[1] * 64 +
    //     part[2] * 32 +
    //     part[3] * 16 +
    //     part[4] * 8 +
    //     part[5] * 4 +
    //     part[6] * 2 +
    //     part[7] * 1;
    sendImageData[index++] = temp;
  }
  return {
    array: Array.from(sendImageData),
    width: width1,
    height: sendHeight,
  };
};